package com.csun.cmny.provider.ice;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-27 20:08
 * @Description: the class ice server listener
 */
@Slf4j
@Component
@ChannelHandler.Sharable
public class IceServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket packet) throws Exception {

        try {
            ByteBuf buf = (ByteBuf) packet.copy().content();
            byte[] req = new byte[buf.readableBytes()];
            buf.readBytes(req);

            String data = new String(req, "UTF-8");
            log.info("# ice read...：   [{}] ...", data);
        } catch (Exception e) {
            log.error("=====>  ice channel read error: {}", e.getMessage());
        } finally {
            // 抛弃收到的数据
            ReferenceCountUtil.release(packet);
        }

    }
}